package vuelta.general.stage;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import vuelta.general.R;
import vuelta.general.TabBaseActivity;
import vuelta.general.stage.zoom.AltimetriaZoomActivity;
import vuelta.general.stage.zoom.PlanimetriaZoomActivity;
import vuelta.general.util.Commons;
import vuelta.general.util.Parser;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import appReate.ViewUtility.PixDipUtility;

public class TabView extends TabBaseActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_static_view);
		initToolbar();

		String fromDetail = getIntent().getStringExtra("fromDetail");

		Resources res = getResources();
		TabHost tabHost = getTabHost();
		TabHost.TabSpec spec;
		Intent intent;

		JSONArray jsonArray = Commons.getInstance().getTappeArrayJson();
		int stageNumber = Commons.getInstance().getSelectItemPosition();
		JSONObject jsonObject = null;

		try {
			jsonObject = (JSONObject) jsonArray.get(stageNumber - 1);
		} catch (JSONException e1) {
			e1.printStackTrace();
		}

		String tappaNumero = "";
		try {
			tappaNumero = jsonObject.getString("tappa");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		TextView tappaText = (TextView) findViewById(R.id.nome_tappa_text);

		if (!tappaNumero.equals("0")) {
			tappaText.setText(getResources().getString(R.string.Stage_Lowercase) + " " + tappaNumero);
		} else {
			tappaText.setText(getResources().getString(R.string.Stage_Prologue_lowercase));
		}

		intent = new Intent().setClass(this, PlanimetriaZoomActivity.class);
		spec = tabHost.newTabSpec("Planimetria").setIndicator(getResources().getString(R.string.Planimetry_Lowercase))
				.setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent().setClass(this, AltimetriaZoomActivity.class);
		spec = tabHost.newTabSpec("Altimetria").setIndicator(getResources().getString(R.string.Altimetry_Lowercase))
				.setContent(intent);
		tabHost.addTab(spec);

		JSONArray SalitejsonArray = Parser.getInstance().getAttributeFromJsonStage(jsonArray, stageNumber, "salite");

		if (SalitejsonArray.length() > 0) {
			intent = new Intent().setClass(this, SaliteActivity.class);
			spec = tabHost.newTabSpec("Salite").setIndicator(getResources().getString(R.string.Climbs_Lowercase))
					.setContent(intent);
			tabHost.addTab(spec);
			// TODO dinamico a seconda delle dimensioni display
			// Cambiare dimensione tab

			int pixWidth = PixDipUtility.getPIXwidth(this);
			pixWidth = pixWidth / 3;
			int pixheight = PixDipUtility.getPIXheight(this);
			pixheight = pixheight / 11;

			tabHost.getTabWidget().getChildAt(0).setLayoutParams(new LinearLayout.LayoutParams(pixWidth, pixheight));
			tabHost.getTabWidget().getChildAt(1).setLayoutParams(new LinearLayout.LayoutParams(pixWidth, pixheight));
			tabHost.getTabWidget().getChildAt(2).setLayoutParams(new LinearLayout.LayoutParams(pixWidth, pixheight));
		} else {
			int pixWidth = PixDipUtility.getPIXwidth(this);
			pixWidth = pixWidth / 2;
			int pixheight = PixDipUtility.getPIXheight(this);
			pixheight = pixheight / 11;

			// Cambiare dimensione tab
			tabHost.getTabWidget().getChildAt(0).setLayoutParams(new LinearLayout.LayoutParams(pixWidth, pixheight));
			tabHost.getTabWidget().getChildAt(1).setLayoutParams(new LinearLayout.LayoutParams(pixWidth, pixheight));
		}

		if (fromDetail.equals("Salite")) {
			tabHost.setCurrentTabByTag("Salite");
		} else if (fromDetail.equals("Altimetria")) {
			tabHost.setCurrentTabByTag("Altimetria");
		} else {
			tabHost.setCurrentTabByTag("Planimetria");
		}

		TabWidget tw = getTabWidget();
		for (int i = 0; i < tw.getChildCount(); i++) {
			RelativeLayout v = (RelativeLayout) tw.getChildAt(i);
			v.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_selector));
			TextView lf = (TextView) v.getChildAt(1);
			lf.setTextSize(16);
			lf.setTextColor(getResources().getColor(R.color.bianco));
			lf.setPadding(0, 0, 0, 4);
		}

		// setTabColor(tabHost);

		// Change background
		// for(int i=0; i<tabHost.getChildCount(); i++)
		// tabHost.getChildAt(i).setBackgroundResource(R.drawable.tab_selector);

	}

	// public static void setTabColor(TabHost tabhost) {
	// for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)
	// {
	// tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FF0000"));
	// //unselected
	// }
	// tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("#0000FF"));
	// // selected
	// }

}
